package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.jeecg.modules.qyxx.entity.BgHzFjdSbZzsqysds;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.mapper.BgHzFjdSbZzsqysdsMapper;
import org.jeecg.modules.qyxx.service.IBgHzFjdSbZzsqysdsService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 增值税跟企业所得税分季度汇总趋势
 * @Author: jeecg-boot
 * @Date: 2022-05-10
 * @Version: V1.0
 */
@Service
public class BgHzFjdSbZzsqysdsServiceImpl extends MppServiceImpl<BgHzFjdSbZzsqysdsMapper, BgHzFjdSbZzsqysds> implements IBgHzFjdSbZzsqysdsService {
    private static final String ZZS_ZSXM_DM = "10101";
    private static final String QYSDS_ZSXM_DM = "10104";

    @Override
    public Map<String, Object> zzsAndQysdsHz(BgNsrjbxxReq req) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> zzsList = new ArrayList<>();
        List<Map<String, Object>> qysdsList = new ArrayList<>();

        DateUtils utils = DateUtils.getInstance();
        List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());

        for (String tjnd : tjnds) {
            Map<String, Object> zzsMap = new HashMap<>();
            Map<String, Object> qysdsMap = new HashMap<>();
            List<BgHzFjdSbZzsqysds> zzs = new ArrayList<>();
            List<BgHzFjdSbZzsqysds> qysds = new ArrayList<>();
            LambdaQueryWrapper<BgHzFjdSbZzsqysds> wrapper = Wrappers.lambdaQuery(BgHzFjdSbZzsqysds.class);
            wrapper.eq(BgHzFjdSbZzsqysds::getDjxh, req.getDjxh()).eq(BgHzFjdSbZzsqysds::getTjnd, tjnd);
            List<BgHzFjdSbZzsqysds> list = super.list(wrapper);
            for (BgHzFjdSbZzsqysds bgHzFjdSbZzsqysds : list) {
                String zsxmDm = bgHzFjdSbZzsqysds.getZsxmDm();
                String year = bgHzFjdSbZzsqysds.getTjnd();
                bgHzFjdSbZzsqysds.setYbtse(bgHzFjdSbZzsqysds.getYbtse().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                if (ZZS_ZSXM_DM.equals(zsxmDm) && tjnd.equals(year)) {
                    zzs.add(bgHzFjdSbZzsqysds);
                } else if (QYSDS_ZSXM_DM.equals(zsxmDm) && tjnd.equals(year)) {
                    qysds.add(bgHzFjdSbZzsqysds);
                }
            }
            zzsMap.put("zzsData", zzs);
            zzsMap.put("tjnd", tjnd);
            qysdsMap.put("qysdsData", qysds);
            qysdsMap.put("tjnd", tjnd);
            zzsList.add(zzsMap);
            qysdsList.add(qysdsMap);
        }
        map.put("zzs", zzsList);
        map.put("qysds", qysdsList);
        return map;
    }
}
